package com.bitdf.txing.seckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bitdf.txing.common.to.SkuReductionTo;
import com.bitdf.txing.seckill.dao.SkuFullReductionDao;
import com.bitdf.txing.seckill.entity.SkuFullReductionEntity;
import com.bitdf.txing.seckill.entity.SkuLadderEntity;
import com.bitdf.txing.seckill.service.SkuFullReductionService;
import com.bitdf.txing.seckill.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    SkuLadderService skuLadderService;

    /**
     * 在创建商品保存sku的满减、满打折信息
     * @param skuReductionTo
     */
    @Override
    @Transactional
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        //1、sku的优惠、满减等信息
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
        skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
        skuLadderEntity.setDiscount(skuReductionTo.getDiscount());
        skuLadderEntity.setAddOther((byte)skuReductionTo.getCountStatus());
        if(skuReductionTo.getFullCount() > 0) {
            skuLadderService.save(skuLadderEntity);
        }
        //2、sms_sku_full_reduction
        SkuFullReductionEntity reductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTo,reductionEntity);
        if(reductionEntity.getFullPrice().compareTo(new BigDecimal("0"))==1){
            this.save(reductionEntity);
        }
    }

}